package com.elinkway.infinitemovies.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import java.io.BufferedReader;
import java.io.FileReader;

/**
 * Created by huangxiaojian on 2017/6/1.
 *
 * 手机信息相关工具类
 */

public class PhoneUtils {
    public static int STANDARD_RAM = 2; // 基准RAM数

    /**
     * 运营商代码 移动、联通、电信
     * @param context 上下文
     * @return 返回前五位标识
     */
    public static String getOperator(Context context) {
        TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = telManager.getSubscriberId();
        if (StringUtil.isEmpty(imsi) || "null".equalsIgnoreCase(imsi) || "unknow".equalsIgnoreCase(imsi)) {
            return "46000";
        }
        return imsi.substring(0, 5);

    }

    /**
     * 获取设备型号
     */
    public static String getModel() {
        return android.os.Build.MODEL;
    }

    public static String getManufacturer() {
        return android.os.Build.MANUFACTURER;
    }

    /**
     * 获取手机imei号
     * @param context 上下文
     * @return
     */
    public static String getIMEI(Context context) {
        try {
            String deviceId = ((TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
            if (null == deviceId || deviceId.length() <= 0) {
                return "";
            } else {
                return deviceId.replace(" ", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取mac地址
     * @param context 上下文
     * @return
     */
    public static String getMacAddress(Context context) {
        String macAddress = null;
        WifiInfo wifiInfo = ((WifiManager) context
                .getSystemService(Context.WIFI_SERVICE)).getConnectionInfo();
        if (wifiInfo != null) {
            macAddress = wifiInfo.getMacAddress();
            if (macAddress == null || macAddress.length() <= 0) {
                return "";
            } else {
                return macAddress;
            }
        } else {
            return "";
        }
    }
    public static boolean isTotalRAMThan2G(Context context){
        if (context != null && getTotalRam(context) > STANDARD_RAM) {
            return true;
        }
        return false;
    }

    /**
     * 返回GB内存数
     * @param context
     * @return
     */
    public static int getTotalRam(Context context){
        String path = "/proc/meminfo";
        String firstLine = null;
        int totalRam = 0 ;
        try{
            FileReader fileReader = new FileReader(path);
            BufferedReader br = new BufferedReader(fileReader,8192);
            firstLine = br.readLine().split("\\s+")[1];
            br.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        if(firstLine != null){
            totalRam = (int)Math.ceil((new Float(Float.valueOf(firstLine) / (1024 * 1024)).doubleValue()));
        }

        return totalRam;//返回GB
    }
}
